package jude.java.money.tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dig {

	public static List<String> dig(String content, int max) {
		String regex = "\\b[A-Za-z][a-z]{3,15}\\b";
		int n = 0;
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(content);
		final Map<String, Integer> all = new HashMap<String, Integer>();
		while (m.find(n)) {
			String word = m.group(0).toLowerCase();
			if (word.length() > 3) {
				Integer current = all.get(word);
				if (current == null)
					all.put(word, 1);
				else
					all.put(word, current+1);
			}
			n = m.end(0);
		}
		ArrayList<String> words = new ArrayList<String>();
		words.addAll(all.keySet());
		for (int i=0, j=words.size(); i<j; i++) {
			String word = words.get(i);
			String w = prototype(word);
			if (w == word)
				continue;
			Integer n1 = all.get(word);
			Integer n2 = all.get(w);
			if (n2 == null) {
				all.put(w, n1);
				words.add(w);
			} else {
				all.put(w, n1 + n2);
			}
			all.put(word, 0);
		}
		Collections.sort(words, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return all.get(o2) - all.get(o1);
			}
		});
		int total = words.size();
		List<String> keywords = new ArrayList<String>();
		int valid = 0;
		int last = 0;
		for (int i = 0; i < total && (max == 0 || valid < max); i++) {
			String w = words.get(i);
			if (ignore(w))
				continue;
			int count = all.get(w);
			if (count <= 1)
				break;
			if (last - count >= 10)
				break;
			last = count;
			keywords.add(w);
			valid ++;
		}
		return keywords;
	}
	
	private static boolean ignore(String word) {
		for (int i = 0; i < NON_KEYWORD.length; i++) {
			if (NON_KEYWORD[i].equals(word))
				return true;
		}
		return false;
	}
	
	/**
	 * 名词复数, 副词 的原型
	 * */
	private static String prototype(String word) {
		if (word == null)
			return word;
		int len = word.length();
		if (len < 4)
			return word;
		if (word.charAt(len-1) == 's') {
			char c = word.charAt(len - 2);
			//success, this, photo
			if (c == 's' || c == 'i' || c == 'a' || c == 'u')
				return word;
			return word.substring(0, len-1);
		} else if (word.endsWith("ly")) {
			if (word.endsWith("ally"))
				return word.substring(0, len-2);
			return word;
		}
		return word;
	}
	
	/*
	 * 转换成wprobot过滤词(Exclude from Tags)	\s*"(\w+)",		->		$s1;
	 * */
	public static final String[] NON_KEYWORD = {
		"about",
		"above",
		"along",
		"also",
		"among",
		"around",
		"back",
		"been",
		"before",
		"behind",
		"below",
		"beside",
		"besids",
		"between",
		"beyond",
		"china",
		"could",
		"does",
		"down",
		"during",
		"every",
		"except",
		"exclude",
		"first",
		"five",
		"four",
		"from",
		"have",
		"here",
		"http",
		"inside",
		"into",
		"just",
		"like",
		"many",
		"maybe",
		"more",
		"much",
		"near",
		"need",
		"next",
		"only",
		"onto",
		"outside",
		"over",
		"round",
		"said",
		"self",
		"seven",
		"since",
		"still",
		"than",
		"thank",
		"that",
		"their",
		"them",
		"there",
		"these",
		"they",
		"this",
		"three",
		"through",
		"till",
		"toward",
		"towards",
		"under",
		"until",
		"upon",
		"want",
		"were",
		"what",
		"when",
		"where",
		"which",
		"will",
		"with",
		"within",
		"without",
		"would",
		"your",
		"yourself",
	};
	
}
